package com.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.DB;
import com.sun.net.httpserver.Authenticator.Result;
import com.tools.Constants;

public class S2S4Users extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		Constants.init();
		System.out.println("DB name:" + Constants.DBNAME);
		DB.connDB();
		super.init();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		Enumeration<String> er = request.getParameterNames();
		System.out.println("[S2S4] S2S4 get begin");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		String phone_number = null;
		String imgPath = null;
		String people = null;
		String event = null;
		String cmd = null;

		while (er.hasMoreElements()) {
			String element = er.nextElement();
			if (element.equals("phone_number")) {
				phone_number = request.getParameterValues("phone_number")[0];
				System.out.println("[S2S4] phone_number:" + phone_number);
			}
			if (element.equals("cmd")) {
				cmd = request.getParameterValues("cmd")[0];
				System.out.println("[S2S4] cmd:" + cmd);
			}
			if (element.equals("people")) {
				people = request.getParameterValues("people")[0];
				System.out.println("[S2S4] people:" + people);
			}
			if (element.equals("event")) {
				event = request.getParameterValues("event")[0];
				System.out.println("[S2S4] event:" + event);
			}
			if (element.equals("imgPath")) {
				imgPath = request.getParameterValues("imgPath")[0];
				System.out.println("[S2S4] imgPath:" + imgPath);
			}
		}

		if ((phone_number != null)) {
			ResultSet rs = DB.select("select * from user where phone_number='" + phone_number + "';");
			try {
				while (rs.next()) {
					String phone_number1 = rs.getString("phone_number");
					String user_id1 = rs.getString("_id");
					if (phone_number1.equals(phone_number)) {
						writer.println(user_id1);
						System.out.println("用户:" + phone_number1 + " ID:" + user_id1 + "请求连接");
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		if ((people != null) && (people.equals("new")) && (cmd != null) && (cmd.equals("1"))) {
			String phone_number1 = null;
			String my_story1 = null;
			String img_path1 = null;
			ResultSet rs = DB.select("select * from userinfo,user where userinfo.user_id=user._id order by time desc limit 0,25;");
			try {
				while (rs.next()) {
					phone_number1 = rs.getString("phone_number");
					my_story1 = rs.getString("my_story");
					img_path1 = rs.getString("img_path");
					writer.println(phone_number1 + "屙" + my_story1 + "屙" + img_path1 + "莪");
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if ((event != null) && (event.equals("new")) && (cmd != null) && (cmd.equals("2"))) {
			String phone_number2 = null;
			String e_img_path1 = null;
			String e_story1 = null;
			System.out.println("web request event list");
			ResultSet rs2 = DB.select("select * from event,user where event.e_user_id=user._id order by e_create_time desc limit 0,10;");
			try {
				while (rs2.next()) {
					phone_number2 = rs2.getString("phone_number");
					e_img_path1 = rs2.getString("e_img_path");
					e_story1 = rs2.getString("e_story");
					System.out.println(phone_number2 + "," + e_story1 + "," + e_img_path1 + ",");
					writer.println("标价物品" + "屙" + e_story1 + "屙" + e_img_path1 + "莪");
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

	}

}
